using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DatabaseProcess;

/// <summary>
/// Summary description for clsAdvertisement
/// </summary>
public class clsAdvertisement
{
    private int _id;

    public int Id
    {
        get { return _id; }
        set { _id = value; }
    }
    private string _url;

    public string Url
    {
        get { return _url; }
        set { _url = value; }
    }
    private string _image_url;

    public string Image_url
    {
        get { return _image_url; }
        set { _image_url = value; }
    }
    private DateTime _begin_date;

    public DateTime Begin_date
    {
        get { return _begin_date; }
        set { _begin_date = value; }
    }
    private DateTime _finish_date;

    public DateTime Finish_date
    {
        get { return _finish_date; }
        set { _finish_date = value; }
    }
    private string _name;

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
    private string _phone_number;

    public string Phone_number
    {
        get { return _phone_number; }
        set { _phone_number = value; }
    }
    private string _address;

    public string Address
    {
        get { return _address; }
        set { _address = value; }
    }
    private string _information;

    public string Information
    {
        get { return _information; }
        set { _information = value; }
    }
    
	public clsAdvertisement()
	{
        this.Begin_date = DateTime.Now;
        this.Finish_date = Convert.ToDateTime("1/1/2010");
	}
    public int insertAds()
    {
        DBconnect connect = new DBconnect();
        connect.Open();
        if (connect.IsConnected == false)
            return -1;
        string[] listcol = new string[] { "url", "image_url", "begin_date", "finish_date", "name", "information", "address", "phone_number" };
        object[] listvalue = new object[] {this.Url, this.Image_url, this.Begin_date, this.Finish_date, this.Name, this.Information, this.Address, this.Phone_number };
        int i = connect.Insert("advertisement", listcol, listvalue);
        connect.Close();
        return i;
    }
    public int updateAds()
    {
        DBconnect connect = new DBconnect();
        connect.Open();
        if (connect.IsConnected == false)
            return -1;
        string[] listcol = new string[] { "url", "image_url", "begin_date", "finish_date", "name", "information", "address", "phone_number" };
        object[] listvalue = new object[] { this.Url, this.Image_url, this.Begin_date, this.Finish_date, this.Name, this.Information, this.Address, this.Phone_number };

        object[] ob = new object[] { "@ID", Id };
        int i = connect.Update("advertisement", listcol, listvalue, "ID=@ID", ob);                
        connect.Close();
        return i;
    }

    public DataTable GetAdver()
    {
        DBconnect db = new DBconnect();
        db.Open();
        DataTable kq = new DataTable();
        string sql = "select top 4 image_url,address from advertisement where isDelete = '0' ORDER BY NEWID()";
        kq = db.GetDataTable(sql);
        return kq;


    }
    public DataTable getTableAdv()
    {
        DBconnect connet = new DBconnect();
        connet.Open();
        if (connet.IsConnected == false)
            return null;
        DataTable result = new DataTable();
        string sql = "SELECT id, url, image_url, begin_date, finish_date, [name], information, address, phone_number, isDelete  from advertisement  ORDER BY [name]";
        result = connet.GetDataTable(sql);
        connet.Close();
        return result;
    }
    public bool active_unactive()
    {
        bool result = false;
        DBconnect connect = new DBconnect();
        connect.Open();
        if (connect.IsConnected == false)
            return result;
        object[] ob = new object[] { "@ID", Id };
        DataTable tmp = connect.GetDataTable("advertisement", "ID, isDelete", "ID=@ID", ob);
        if (tmp.Rows.Count > 0)
        {
            int active = 0;
            if (Convert.ToInt32(tmp.Rows[0]["isDelete"].ToString()) == 0)
            {
                active = 1;
            }
            string sql = "UPDATE advertisement SET isDelete = " + active + " WHERE ID = " + Id;
            connect.Execute(sql);
            result = true;
        }
        connect.Close();
        return result;
    }
    public clsAdvertisement GetAdv(int id)
    {
        clsAdvertisement adv = new clsAdvertisement();

        DBconnect connet = new DBconnect();
        connet.Open();
        if (connet.IsConnected == false)
            return null;

        string sqlString = "select* from advertisement where id=" + id;
        DataTable result = connet.GetDataTable(sqlString);
        if (result.Rows.Count == 0)
        {
            connet.Close();
            return null;
        }
        else
        {
            adv.Id = id;
            adv.Image_url = result.Rows[0]["image_url"].ToString();
            adv.Information = result.Rows[0]["information"].ToString();
            adv.Name = result.Rows[0]["name"].ToString();
            adv.Phone_number = result.Rows[0]["phone_number"].ToString();
            adv.Url = result.Rows[0]["url"].ToString();
            adv.Finish_date = DateTime.Parse(result.Rows[0]["finish_date"].ToString());
            adv.Begin_date = DateTime.Parse(result.Rows[0]["begin_date"].ToString());
            adv.Address = result.Rows[0]["address"].ToString();
            connet.Close();
        }

        return adv;
    }
}
